package com.hopu.oa.controller.UserManage;

import com.hopu.oa.entity.user.AoaDept;
import com.hopu.oa.entity.user.AoaPosition;
import com.hopu.oa.entity.user.AoaUser;
import com.hopu.oa.service.impl.UserManageControllerImpl.AoaUserLoginRecordServiceImpl;
import com.hopu.oa.service.impl.UserManageControllerImpl.DeptManageServiceImp;
import com.hopu.oa.service.impl.UserManageControllerImpl.OnlineUserManageServiceImp;
import com.hopu.oa.service.impl.user.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
public class OnlineUserManageController {

    @Autowired
    private OnlineUserManageServiceImp onlineUserManageServiceImp;
    @Autowired
    private AoaUserLoginRecordServiceImpl aoaUserLoginRecordService;
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private DeptManageServiceImp deptManageServiceImp;

    /**
     * 外层分页
     *
     * @param page
     * @param size
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/positionmanage")
    public String positionMain(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "size", defaultValue = "8") Integer size, AoaPosition aoaPosition, HttpSession session, Model model, String find) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        System.out.println("find = " + find);
        aoaPosition.setName(find);
        //总数量
        int totalElements = onlineUserManageServiceImp.queryAllByPosition(aoaPosition, 0, 0).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaPosition> aoaPositions = onlineUserManageServiceImp.queryAllByPosition(aoaPosition, (page - 1) * size, size);
        HashMap<String, Integer> map1 = new HashMap<>();
        map1.put("totalElements", totalElements);
        map1.put("totalPages", totalPages);
        map1.put("number", page);
        map1.put("size", size);

        model.addAttribute("page", map1);
        model.addAttribute("positionlist", aoaPositions);
        model.addAttribute("find", find);
        model.addAttribute("url", "positiontable");
        return "user/positionmanage";
    }

    /**
     * 内层分页
     *
     * @param page
     * @param size
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/positiontable")
    public String position(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "size", defaultValue = "8") Integer size, AoaPosition aoaPosition, HttpSession session, Model model, String find) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        System.out.println("find = " + find);
        aoaPosition.setName(find);
        //总数量
        int totalElements = onlineUserManageServiceImp.queryAllByPosition(aoaPosition, 0, 0).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaPosition> aoaPositions = onlineUserManageServiceImp.queryAllByPosition(aoaPosition, (page - 1) * size, size);
        System.out.println(aoaPositions);
        HashMap<String, Integer> map2 = new HashMap<>();
        map2.put("totalElements", totalElements);
        map2.put("totalPages", totalPages);
        map2.put("number", page);
        map2.put("size", size);

        model.addAttribute("page", map2);
        model.addAttribute("positionlist", aoaPositions);
        model.addAttribute("find", find);
        model.addAttribute("url", "positiontable");
        return "user/positiontable";
    }

    /**
     * 新增修改跳转
     *
     * @param positionid 主键
     * @return 单条数据
     */
    @RequestMapping("positionedit")
    public String queryById(Integer positionid, Model model, Integer page) {
        System.out.println("page = " + page);
        List<AoaDept> aoaDepts = deptManageServiceImp.findAllDepts();
        model.addAttribute("depts", aoaDepts);
        if (positionid != null) {
            AoaPosition aoaPosition = onlineUserManageServiceImp.queryById(positionid);
            System.out.println(aoaPosition);
            model.addAttribute("position", aoaPosition);
            model.addAttribute("page", page);
            return "user/positionedit";
        } else {
            return "user/positionedit";
        }
    }


    /**
     * 新增/修改数据
     *
     * @param aoaPosition 实体
     * @return 新增结果
     */
    @RequestMapping("positioneditOperating")
    public String add(AoaPosition aoaPosition, Integer page, Model model) {
        System.out.println("职位名字" + aoaPosition.getName());
        System.out.println(aoaPosition);
        AoaPosition aoaPosition1 = onlineUserManageServiceImp.selectByName(aoaPosition.getName());
        AoaPosition aoaPosition2 = onlineUserManageServiceImp.selectById(aoaPosition.getPositionId());
        if (aoaPosition.getPositionId() != null) {
            if (aoaPosition1 != null && aoaPosition2.getName().equals(aoaPosition.getName())) {
                System.out.println("修改");
                onlineUserManageServiceImp.update(aoaPosition);
                return "redirect:positionmanage?page=" + page;
            } else {
                System.out.println("职位已存在");
                model.addAttribute("errormess", "职位已存在");
                List<AoaDept> aoaDepts = deptManageServiceImp.findAllDepts();
                model.addAttribute("depts", aoaDepts);
                return "user/positionedit";
            }
        } else {
            if (aoaPosition1 != null) {
                System.out.println("职位已存在");
                model.addAttribute("errormess", "职位已存在");
                List<AoaDept> aoaDepts = deptManageServiceImp.findAllDepts();
                model.addAttribute("depts", aoaDepts);
                return "user/positionedit";
            } else {
                System.out.println("新增");
                onlineUserManageServiceImp.insert(aoaPosition);
                return "redirect:positionmanage";
            }
        }
    }

    /**
     * 删除数据
     *
     * @param positionId 主键
     * @return 新增结果
     */
    @RequestMapping("deletePosition")
    public String delete(Integer positionId, Model model, Integer page) {
        System.out.println("positionId = " + positionId);
        int size = userService.deletePosition(positionId).size();
        System.out.println(size);
        if (size > 0) {
            System.out.println("不能删除");
            //错误提示
            model.addAttribute("errormess", "该职位存在职员，不能删除");
            return "user/positionmanage?page=" + page;
        } else {
            System.out.println("可以删除");
            onlineUserManageServiceImp.delete(positionId);
            return "redirect:positionmanage?page=" + page;
        }
    }


    /**
     * 在线用户分页搜索
     *
     * @param aoaUser
     * @param page
     * @param size
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/morelogrecord")
    public String userlogrecordmanageMain(AoaUser aoaUser, @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "8") int size, Model model, HttpSession session, String find) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        aoaUser.setUserName(find);
        //总数量
        int totalElements = aoaUserLoginRecordService.queryAllByLimit(aoaUser, page, 0).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaUser> aoaUserLoginRecords = aoaUserLoginRecordService.queryAllByLimit(aoaUser, (page - 1) * size, size);
        HashMap<String, Integer> map1 = new HashMap<>();
        map1.put("totalElements", totalElements);
        map1.put("totalPages", totalPages);
        map1.put("number", page);
        map1.put("size", size);
        model.addAttribute("page", map1);
        model.addAttribute("userloglist", aoaUserLoginRecords);
        model.addAttribute("find", find);
        model.addAttribute("url", "userlogrecordmanagetable");
        return "user/userlogrecordmanage";
    }

    /**
     * 在线用户分页搜索
     * 内分页
     *
     * @param aoaUser
     * @param page
     * @param size
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/userlogrecordmanagetable")
    public String userlogrecordmanagetable(AoaUser aoaUser, @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "8") int size, Model model, HttpSession session, String find) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        aoaUser.setUserName(find);

        //总数量
        int totalElements = aoaUserLoginRecordService.queryAllByLimit(aoaUser, page, 0).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaUser> aoaUserLoginRecords = aoaUserLoginRecordService.queryAllByLimit(aoaUser, (page - 1) * size, size);
        HashMap<String, Integer> map1 = new HashMap<>();
        map1.put("totalElements", totalElements);
        map1.put("totalPages", totalPages);
        map1.put("number", page);
        map1.put("size", size);
        model.addAttribute("page", map1);
        model.addAttribute("userloglist", aoaUserLoginRecords);
        model.addAttribute("find", find);
        model.addAttribute("url", "userlogrecordmanagetable");
        return "user/userlogrecordmanagetable";
    }


}
